  package com.e2e.haiti.infrastructure.surveyor;

  import com.actionbarsherlock.app.SherlockFragment;
import com.e2e.haiti.infrastructure.surveyor.R;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class FragmentPrimaryQuestionnaire extends SherlockFragment {
	
    private Utils utilsHelper = null;
    private Cursor structureCursor = null;
    private int structureID;
	
	private EditText EditText_SystemConditionNote  = null;
	private EditText EditText_ExteriorWallMaterialNote  = null;    
	private EditText EditText_ExteriorWallConditionNote  = null;
	private EditText EditText_ExteriorWallThickness  = null;    
	private EditText EditText_StructuralSystemNote  = null; 
	private EditText EditText_ColumnConstructionMaterialNote  = null;    
	private EditText EditText_FullColumnWidth  = null;    
	private EditText EditText_FullColumnDepth  = null;    
	private EditText EditText_ColumnRebarCageWidth  = null;    
	private EditText EditText_ColumnRebarCageDepth= null;    
	private EditText EditText_ColumnLongitudinaBarsNumber  = null;    
	private EditText EditText_ColumnMinimumTransverseSpacing  = null;    
	private EditText EditText_BeamConstructionMaterialNote  = null;    
	private EditText EditText_FullBeamWidth  = null; 
	private EditText EditText_FullBeamDepth= null;    
	private EditText EditText_BeamRebarCageWidth  = null;    
	private EditText EditText_BeamRebarCageDepth= null;    
	private EditText EditText_BeamLongitudinaBarsNumber  = null;    
	private EditText EditText_BeamMinimumTransverseSpacing  = null;    

    private Spinner Spinner_SystemCondition = null;
    private Spinner Spinner_ExteriorWallCondition = null;
    private Spinner Spinner_StructuralSystem = null;
    private Spinner Spinner_ColumnLongitudinaBarsSize = null;
    private Spinner Spinner_ColumnBarTexture = null;
    private Spinner Spinner_ColumnHookDetail = null;
    private Spinner Spinner_BeamLongitudinaBarsSize = null;
    private Spinner Spinner_BeamBarTexture = null;
    private Spinner Spinner_BeamHookDetail = null;

    private CheckBox CheckBox_ExteriorWallMaterial_Masonry = null;
    private CheckBox CheckBox_ExteriorWallMaterial_Stone = null;
    private CheckBox CheckBox_ExteriorWallMaterial_Earth = null;
    private CheckBox CheckBox_ExteriorWallMaterial_Wood = null;
    private CheckBox CheckBox_ExteriorWallMaterial_Metal = null;
    private CheckBox CheckBox_ExteriorWallMaterial_Other= null;
    private CheckBox CheckBox_WallReinforcement = null;
    private CheckBox CheckBox_ColumnsPresent = null;
    private CheckBox CheckBox_RingBeam = null;
    private CheckBox CheckBox_Concrete_Column = null;
    private CheckBox CheckBox_Metal_Column = null;
    private CheckBox CheckBox_Wood_Column = null;
    private CheckBox CheckBox_Bamboo_Column	 = null;
    private CheckBox CheckBox_Other_Column = null;
    private CheckBox CheckBox_Concrete_Beam= null;
    private CheckBox CheckBox_Metal_Beam= null;
    private CheckBox CheckBox_Wood_Beam= null;
    private CheckBox CheckBox_Bamboo_Beam= null;
    private CheckBox CheckBox_Other_Beam= null;    

    private static final int SYSTEM_CONDITION_ADAPTOR_ID = 0;
    private static final int EXTERIOR_WALL_CONDITION_ADAPTOR_ID = 1;
    private static final int STRUCTURAL_SYSTEM_ADAPTOR_ID = 2;
    private static final int COLUMN_LONGITUDINAL_BAR_SIZE_ADAPTOR_ID = 3;
    private static final int COLUMN_BAR_TEXTURE_ADAPTOR_ID = 4;
    private static final int COLUMN_HOOK_DETAIL_ADAPTOR_ID = 5;
    private static final int BEAM_LONGITUDINAL_BAR_SIZE_ADAPTOR_ID = 6;
    private static final int BEAM_BAR_TEXTURE_ADAPTOR_ID = 7;
    private static final int BEAM_HOOK_DETAIL_ADAPTOR_ID = 8;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                
        utilsHelper = new Utils(getActivity().getApplicationContext());              
                 
    } // end of the onCreate method
    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	
    	// Sets the cursor
        setStructureCursor(); 
    	
    	View fragmentView = inflater.inflate(R.layout.primary_questionnaire_layout, container, false);
    	manageFormFields(fragmentView);
    	
    	return fragmentView;
    }
    
    private void setStructureCursor(){
        utilsHelper.openDatabase(); 
   	   // Get structure ID
          structureID = getActivity().getIntent().getIntExtra("structureID", -1); //Get structureID otherwise -1      
          structureCursor = utilsHelper.getStructureCursor(structureID); 
          utilsHelper.closeDatabase(); 
      }
    
    private void manageFormFields(View v){

    	// Declares the text input fields
		EditText_SystemConditionNote = (EditText) v.findViewById(R.id.Primary_editText_SystemConditionNote);
		EditText_ExteriorWallMaterialNote = (EditText) v.findViewById(R.id.Primary_editText_ExteriorWallMaterialNote);    
		EditText_ExteriorWallConditionNote = (EditText) v.findViewById(R.id.Primary_editText_ExteriorWallConditionNote);
		EditText_ExteriorWallThickness = (EditText) v.findViewById(R.id.Primary_editText_WallThickness);    
		EditText_StructuralSystemNote = (EditText) v.findViewById(R.id.Primary_editText_StructuralSystemNote); 
		EditText_ColumnConstructionMaterialNote = (EditText) v.findViewById(R.id.Primary_editText_ColumnConstructionMaterialNote);    
		EditText_FullColumnWidth = (EditText) v.findViewById(R.id.Primary_editText_FullColumnWidth);    
		EditText_FullColumnDepth = (EditText) v.findViewById(R.id.Primary_editText_FullColumnDepth);    
		EditText_ColumnRebarCageWidth = (EditText) v.findViewById(R.id.Primary_editText_ColumnRebarCageWidth);    
		EditText_ColumnRebarCageDepth = (EditText) v.findViewById(R.id.Primary_editText_ColumnRebarCageDepth);    
		EditText_ColumnLongitudinaBarsNumber = (EditText) v.findViewById(R.id.Primary_editText_ColumnLongitudinaBarsNumber);    
		EditText_ColumnMinimumTransverseSpacing = (EditText) v.findViewById(R.id.Primary_editText_ColumnMinimumTransverseSpacing);    
		EditText_BeamConstructionMaterialNote = (EditText) v.findViewById(R.id.Primary_editText_BeamConstructionMaterialNote);    
		EditText_FullBeamWidth = (EditText) v.findViewById(R.id.Primary_editText_FullBeamWidth); 
		EditText_FullBeamDepth = (EditText) v.findViewById(R.id.Primary_editText_FullBeamDepth);    
		EditText_BeamRebarCageWidth = (EditText) v.findViewById(R.id.Primary_editText_BeamRebarCageWidth);    
		EditText_BeamRebarCageDepth = (EditText) v.findViewById(R.id.Primary_editText_BeamRebarCageDepth);    
		EditText_BeamLongitudinaBarsNumber = (EditText) v.findViewById(R.id.Primary_editText_BeamLongitudinaBarsNumber);    
		EditText_BeamMinimumTransverseSpacing = (EditText) v.findViewById(R.id.Primary_editText_BeamMinimumTransverseSpacing);
	    
	    // Declares the checkbox input fields
		CheckBox_ExteriorWallMaterial_Masonry = (CheckBox) v.findViewById(R.id.Primary_checkBox_ExteriorWallMaterial_Masonry);
		CheckBox_ExteriorWallMaterial_Stone = (CheckBox) v.findViewById(R.id.Primary_checkBox_ExteriorWallMaterial_Stone);
		CheckBox_ExteriorWallMaterial_Earth = (CheckBox) v.findViewById(R.id.Primary_checkBox_ExteriorWallMaterial_Earth);
		CheckBox_ExteriorWallMaterial_Wood = (CheckBox) v.findViewById(R.id.Primary_checkBox_ExteriorWallMaterial_Wood);
		CheckBox_ExteriorWallMaterial_Metal = (CheckBox) v.findViewById(R.id.Primary_checkBox_ExteriorWallMaterial_Metal);
		CheckBox_ExteriorWallMaterial_Other = (CheckBox) v.findViewById(R.id.Primary_checkBox_ExteriorWallMaterial_Other);
	    CheckBox_WallReinforcement = (CheckBox) v.findViewById(R.id.Primary_checkBox_WallReinforcement);
	    CheckBox_ColumnsPresent = (CheckBox) v.findViewById(R.id.Primary_checkBox_ColumnsPresent);
	    CheckBox_RingBeam = (CheckBox) v.findViewById(R.id.Primary_checkBox_RingBeam);
	    CheckBox_Concrete_Column = (CheckBox) v.findViewById(R.id.Primary_checkBox_Concrete_Column);
	    CheckBox_Metal_Column = (CheckBox) v.findViewById(R.id.Primary_checkBox_Metal_Column);
	    CheckBox_Wood_Column = (CheckBox) v.findViewById(R.id.Primary_checkBox_Wood_Column);
	    CheckBox_Bamboo_Column	 = (CheckBox) v.findViewById(R.id.Primary_checkBox_Bamboo_Column);
	    CheckBox_Other_Column = (CheckBox) v.findViewById(R.id.Primary_checkBox_Other_Column);
	    CheckBox_Concrete_Beam= (CheckBox) v.findViewById(R.id.Primary_checkBox_Concrete_Beam);
	    CheckBox_Metal_Beam= (CheckBox) v.findViewById(R.id.Primary_checkBox_Metal_Beam);
	    CheckBox_Wood_Beam= (CheckBox) v.findViewById(R.id.Primary_checkBox_Wood_Beam);
	    CheckBox_Bamboo_Beam= (CheckBox) v.findViewById(R.id.Primary_checkBox_Bamboo_Beam);
	    CheckBox_Other_Beam= (CheckBox) v.findViewById(R.id.Primary_checkBox_Other_Beam);   
		
        // Checks if data already exists for Edit Text
        String systemConditionNote = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_PRIMARY_SYSTEM_CONDITION_NOTE));
		String exteriorWallMaterialNote = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_EXTERIOR_WALL_MATERIAL_NOTE));
		String exteriorWallConditionNote = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_EXTERIOR_WALL_CONDITION_NOTE));
		String exteriorWallThickness = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_EXTERIOR_WALL_THICKNESS));
		String structuralSystemNote = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_STRUCTURAL_SYSTEM_NOTE));
		String columnConstructionMaterialNote = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_CONSTRUCTION_MATERIAL_NOTE));
		String fullColumnWidth = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_FULL_COLUMN_WIDTH));
		String fullColumnDepth = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_FULL_COLUMN_DEPTH));
		String columnRebarCageWidth = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_REBAR_CAGE_WIDTH));
		String columnRebarCageDepth= structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_REBAR_CAGE_DEPTH));
		String columnLongitudinalBarNumber = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_REINFORCEMENT_NUMBER_OF_LONGITUDINAL_BARS));
		String columnMinimumTransverseSpacing = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_REINFORCEMENT_MINIMUM_TRANSVERSE_SPACING));
		String beamConstructionMaterialNote = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_CONSTRUCTION_MATERIAL_NOTE));
		String fullBeamWidth = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_FULL_BEAM_WIDTH));
		String fullBeamDepth = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_FULL_BEAM_DEPTH));
		String beamRebarCageWidth = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_REBAR_CAGE_WIDTH));
		String beamRebarCageDepth = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_REBAR_CAGE_DEPTH));
		String beamLongitudinalBarsNumber = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_REINFORCEMENT_NUMBER_OF_LONGITUDINAL_BARS));
		String beamMinimumTransverseSpacing = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_REINFORCEMENT_MINIMUM_TRANSVERSE_SPACING));
		
        // Checks if data already exists for Check Box
		Boolean exteriorWallMasonry = stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_EXTERIOR_WALL_MATERIAL_MASONRY)));
		Boolean exteriorWallStone= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_EXTERIOR_WALL_MATERIAL_STONE)));
		Boolean exteriorWallEarth= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_EXTERIOR_WALL_MATERIAL_EARTH)));
		Boolean exteriorWallWood= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_EXTERIOR_WALL_MATERIAL_WOOD)));
		Boolean exteriorWallMetal= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_EXTERIOR_WALL_MATERIAL_METAL)));
		Boolean exteriorWallOther= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_EXTERIOR_WALL_MATERIAL_OTHER)));
		Boolean wallReinforcement = stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_LOAD_BEARING_WALL_REINFORCEMENT)));
		Boolean columnsPresent = stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_LOAD_BEARING_WALL_COLUMNS_PRESENT)));
		Boolean ringBeam = stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_LOAD_BEARING_WALL_RING_BEAM)));
		Boolean concreteColumn= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_CONSTRUCTION_MATERIAL_CONCRETE)));
		Boolean metalColumn= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_CONSTRUCTION_MATERIAL_METAL)));
		Boolean woodColumn= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_CONSTRUCTION_MATERIAL_WOOD)));
		Boolean bambooColumn= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_CONSTRUCTION_MATERIAL_BAMBOO)));
		Boolean otherColumn= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_CONSTRUCTION_MATERIAL_OTHER)));
		Boolean concreteBeam= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_CONSTRUCTION_MATERIAL_CONCRETE)));
		Boolean metalBeam= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_CONSTRUCTION_MATERIAL_METAL)));
		Boolean woodBeam= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_CONSTRUCTION_MATERIAL_WOOD)));
		Boolean bambooBeam= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_CONSTRUCTION_MATERIAL_BAMBOO)));
		Boolean otherBeam= stringToBool(structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_CONSTRUCTION_MATERIAL_OTHER)));
		
		// Sets the text of the input fields
		EditText_SystemConditionNote.setText(systemConditionNote);
		EditText_ExteriorWallMaterialNote.setText(exteriorWallMaterialNote);    
		EditText_ExteriorWallConditionNote.setText(exteriorWallConditionNote);
		EditText_ExteriorWallThickness.setText(exteriorWallThickness);    
		EditText_StructuralSystemNote.setText(structuralSystemNote); 
		EditText_ColumnConstructionMaterialNote.setText(columnConstructionMaterialNote);    
		EditText_FullColumnWidth.setText(fullColumnWidth);    
		EditText_FullColumnDepth.setText(fullColumnDepth);    
		EditText_ColumnRebarCageWidth.setText(columnRebarCageWidth);    
		EditText_ColumnRebarCageDepth.setText(columnRebarCageDepth);    
		EditText_ColumnLongitudinaBarsNumber.setText(columnLongitudinalBarNumber);    
		EditText_ColumnMinimumTransverseSpacing.setText(columnMinimumTransverseSpacing);    
		EditText_BeamConstructionMaterialNote.setText(beamConstructionMaterialNote);    
		EditText_FullBeamWidth.setText(fullBeamWidth); 
		EditText_FullBeamDepth.setText(fullBeamDepth);   
		EditText_BeamRebarCageWidth.setText(beamRebarCageWidth);    
		EditText_BeamRebarCageDepth.setText(beamRebarCageDepth);   
		EditText_BeamLongitudinaBarsNumber.setText(beamLongitudinalBarsNumber);    
		EditText_BeamMinimumTransverseSpacing.setText(beamMinimumTransverseSpacing);
		
		// Sets the value of the Check Boxes
		CheckBox_ExteriorWallMaterial_Masonry.setChecked(exteriorWallMasonry);
		CheckBox_ExteriorWallMaterial_Stone.setChecked(exteriorWallStone);
		CheckBox_ExteriorWallMaterial_Earth.setChecked(exteriorWallEarth);
		CheckBox_ExteriorWallMaterial_Wood.setChecked(exteriorWallWood);
		CheckBox_ExteriorWallMaterial_Metal.setChecked(exteriorWallMetal);
		CheckBox_ExteriorWallMaterial_Other.setChecked(exteriorWallOther);
	    CheckBox_WallReinforcement.setChecked(wallReinforcement);
	    CheckBox_ColumnsPresent.setChecked(columnsPresent);
	    CheckBox_RingBeam.setChecked(ringBeam);
	    CheckBox_Concrete_Column.setChecked(concreteColumn);
	    CheckBox_Metal_Column.setChecked(metalColumn);
	    CheckBox_Wood_Column.setChecked(woodColumn);
	    CheckBox_Bamboo_Column.setChecked(bambooColumn);
	    CheckBox_Other_Column.setChecked(otherColumn);
	    CheckBox_Concrete_Beam.setChecked(concreteBeam);
	    CheckBox_Metal_Beam.setChecked(metalBeam);
	    CheckBox_Wood_Beam.setChecked(woodBeam);
	    CheckBox_Bamboo_Beam.setChecked(bambooBeam);
	    CheckBox_Other_Beam.setChecked(bambooBeam);  
		
	    // Initializes view spinners
        initializeSpinners(v);
    }   
    
    public static boolean stringToBool(String s) {
    	  if (s == null || s.equals(""))
    	    return false;
    	  else if (s.equals("0"))
    	    return false;
    	  else if (s.equals("1"))
    		return true;
    	  throw new IllegalArgumentException(s+" is not a bool. Only 1 and 0 are.");
    }
    
 private void initializeSpinners(View v){
	 
	 // Spinner position
	 int position = -1;
    	
// System Condition Array Adapter ========================================================================================================================
	     ArrayAdapter<CharSequence> adapter_SystemCondition = ArrayAdapter.createFromResource(
	     		getActivity().getApplicationContext(), R.array.RatingScale, android.R.layout.simple_spinner_item );
	   			adapter_SystemCondition.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
	
	   			Spinner_SystemCondition = (Spinner) v.findViewById(R.id.Primmary_spinner_SystemCondition);
	   			Spinner_SystemCondition.setAdapter(adapter_SystemCondition);
	   			
	   		//  Sets the location of spinner to saved value if there is one\
	   			position= getArrayPosition(SYSTEM_CONDITION_ADAPTOR_ID);
	   			Spinner_SystemCondition.setSelection(position, true);

// Exterior Wall Condition Array Adapter ========================================================================================================================
   	     ArrayAdapter<CharSequence> adapter_ExteriorWallCondition = ArrayAdapter.createFromResource(
   	     		getActivity().getApplicationContext(), R.array.RatingScale, android.R.layout.simple_spinner_item );
   	   			adapter_ExteriorWallCondition.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

   	   			Spinner_ExteriorWallCondition = (Spinner) v.findViewById(R.id.Primmary_spinner_ExteriorWallCondition);
   	   			Spinner_ExteriorWallCondition.setAdapter( adapter_ExteriorWallCondition );
   	   			
   	   	   		//  Sets the location of spinner to saved value if there is one
   	   			position = getArrayPosition(EXTERIOR_WALL_CONDITION_ADAPTOR_ID);
   	   			Spinner_ExteriorWallCondition.setSelection(position, true); 
   	   			
   // Structural System Array Adapter ========================================================================================================================
      ArrayAdapter<CharSequence> adapter_StructuralSystem = ArrayAdapter.createFromResource(
      			getActivity().getApplicationContext(), R.array.StructuralSystem, android.R.layout.simple_spinner_item );
    			adapter_StructuralSystem.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

    			Spinner_StructuralSystem = (Spinner) v.findViewById(R.id.Primmary_spinner_StructuralSystem);
    			Spinner_StructuralSystem.setAdapter( adapter_StructuralSystem );
    			
       	   		//  Sets the location of spinner to saved value if there is one\
   	   			position = getArrayPosition(STRUCTURAL_SYSTEM_ADAPTOR_ID);
   	   			Spinner_StructuralSystem.setSelection(position, true);  
   	    			
// Column Longitudinal Bar Size Array Adapter ========================================================================================================================
	     ArrayAdapter<CharSequence> adapter_ColumnLongitudinaBarsSize = ArrayAdapter.createFromResource(
	     		getActivity().getApplicationContext(), R.array.LongitudinalBarsSize, android.R.layout.simple_spinner_item );
	   			adapter_ColumnLongitudinaBarsSize.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

	   			Spinner_ColumnLongitudinaBarsSize = (Spinner) v.findViewById(R.id.Primmary_spinner_ColumnLongitudinaBarsSize);
	   			Spinner_ColumnLongitudinaBarsSize.setAdapter( adapter_ColumnLongitudinaBarsSize );
	   			
	   	   		//  Sets the location of spinner to saved value if there is one\
   	   			position = getArrayPosition(COLUMN_LONGITUDINAL_BAR_SIZE_ADAPTOR_ID);
   	   			Spinner_ColumnLongitudinaBarsSize.setSelection(position, true); 
   	   	    	   			
   // Column Bar Textrue Array Adapter ========================================================================================================================
      ArrayAdapter<CharSequence> adapter_ColumnBarTexture = ArrayAdapter.createFromResource(
      			getActivity().getApplicationContext(), R.array.BarTexture, android.R.layout.simple_spinner_item );
    			adapter_ColumnBarTexture.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

    			Spinner_ColumnBarTexture = (Spinner) v.findViewById(R.id.Primmary_spinner_ColumnBarTexture);
    			Spinner_ColumnBarTexture.setAdapter( adapter_ColumnBarTexture );
    			
    		//  Sets the location of spinner to saved value if there is one\
    			int functionUsagePosition = getArrayPosition(COLUMN_BAR_TEXTURE_ADAPTOR_ID);
    			Spinner_ColumnBarTexture.setSelection(functionUsagePosition, true); 
   	    	    			
// Column Hook Detail Array Adapter ========================================================================================================================
	     ArrayAdapter<CharSequence> adapter_ColumnHookDetail= ArrayAdapter.createFromResource(
	     		getActivity().getApplicationContext(), R.array.HookDetail, android.R.layout.simple_spinner_item );
	   			adapter_ColumnHookDetail.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

	   			Spinner_ColumnHookDetail = (Spinner) v.findViewById(R.id.Primmary_spinner_ColumnHookDetail);
	   			Spinner_ColumnHookDetail.setAdapter( adapter_ColumnHookDetail);
	   			
	   	   		//  Sets the location of spinner to saved value if there is one\
   	   			position = getArrayPosition(COLUMN_HOOK_DETAIL_ADAPTOR_ID);
   	   			Spinner_ColumnHookDetail.setSelection(position, true); 
   	   	    	    	   			
   // Beam Longitudinal Bar Size Array Adapter ========================================================================================================================
      ArrayAdapter<CharSequence> adapter_BeamLongitudinaBarsSize = ArrayAdapter.createFromResource(
      		getActivity().getApplicationContext(), R.array.LongitudinalBarsSize, android.R.layout.simple_spinner_item );
    			adapter_BeamLongitudinaBarsSize.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

    			Spinner_BeamLongitudinaBarsSize = (Spinner) v.findViewById(R.id.Primmary_spinner_BeamLongitudinaBarsSize);
    			Spinner_BeamLongitudinaBarsSize.setAdapter( adapter_BeamLongitudinaBarsSize );
    			
       	   		//  Sets the location of spinner to saved value if there is one\
   	   			position = getArrayPosition(BEAM_LONGITUDINAL_BAR_SIZE_ADAPTOR_ID);
   	   			Spinner_BeamLongitudinaBarsSize.setSelection(position, true); 
   	    	    	    			
// Beam Bar Texture Array Adapter ========================================================================================================================
	     ArrayAdapter<CharSequence> adapter_BeamBarTexture = ArrayAdapter.createFromResource(
	     		getActivity().getApplicationContext(), R.array.BarTexture, android.R.layout.simple_spinner_item );
	   			adapter_BeamBarTexture.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

	   			Spinner_BeamBarTexture = (Spinner) v.findViewById(R.id.Primmary_spinner_BeamBarTexture);
	   			Spinner_BeamBarTexture.setAdapter( adapter_BeamBarTexture );
	   			
	   	   		//  Sets the location of spinner to saved value if there is one\
   	   			position = getArrayPosition(BEAM_BAR_TEXTURE_ADAPTOR_ID);
   	   			Spinner_BeamBarTexture.setSelection(position, true); 
   	    	    	    	   			
// Beam Hook Detail Array Adapter ========================================================================================================================
      ArrayAdapter<CharSequence> adapter_BeamHookDetail = ArrayAdapter.createFromResource(
      		getActivity().getApplicationContext(), R.array.HookDetail, android.R.layout.simple_spinner_item );
    			adapter_BeamHookDetail.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

    			Spinner_BeamHookDetail = (Spinner) v.findViewById(R.id.Primmary_spinner_BeamHookDetail);
    			Spinner_BeamHookDetail.setAdapter( adapter_BeamHookDetail );
    			
       	   		//  Sets the location of spinner to saved value if there is one\
   	   			position = getArrayPosition(BEAM_HOOK_DETAIL_ADAPTOR_ID);
   	   			Spinner_BeamHookDetail.setSelection(position, true);    	   	    	    	    	   			
    }
 
 	private int getArrayPosition(int spinnerType){
 		
 		int position=0;
 		String value=null;
 		
 		switch (spinnerType){
 		
 			case SYSTEM_CONDITION_ADAPTOR_ID:
 				
 				ArrayAdapter systemCondition_Adapter = (ArrayAdapter) Spinner_SystemCondition.getAdapter();			
 	 			value= structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_PRIMARY_SYSTEM_CONDITION));
 	 			position = systemCondition_Adapter.getPosition(value);
 				break;
 				
 			case EXTERIOR_WALL_CONDITION_ADAPTOR_ID:
 				
 	 			ArrayAdapter exteriorWallCondition_Adapter = (ArrayAdapter) Spinner_ExteriorWallCondition.getAdapter();
 	 			value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_EXTERIOR_WALL_CONDITION));
 	 			position = exteriorWallCondition_Adapter.getPosition(value);
 				break; 	 
 				
 			case STRUCTURAL_SYSTEM_ADAPTOR_ID:
 				
 	 			ArrayAdapter structuralSystem_Adapter = (ArrayAdapter) Spinner_StructuralSystem.getAdapter();
 	 			value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_STRUCTURAL_SYSTEM));
 	 			position = structuralSystem_Adapter.getPosition(value);
 				break; 	 
 				
 			case COLUMN_LONGITUDINAL_BAR_SIZE_ADAPTOR_ID:
 				
 	 			ArrayAdapter columnLongitudinalBarSize_Adapter = (ArrayAdapter) Spinner_ColumnLongitudinaBarsSize.getAdapter();
 	 			value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_REINFORCEMENT_LONGITUDINAL_BAR_SIZE));
 	 			position = columnLongitudinalBarSize_Adapter.getPosition(value);
 				break;
 				
 			case COLUMN_BAR_TEXTURE_ADAPTOR_ID:
 				
 	 			ArrayAdapter columnBarTexture_Adapter = (ArrayAdapter) Spinner_ColumnBarTexture.getAdapter();
 	 			value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_REINFORCEMENT_BAR_TEXTURE));
 	 			position = columnBarTexture_Adapter.getPosition(value);
 				break; 	 
 				
 			case COLUMN_HOOK_DETAIL_ADAPTOR_ID:
 				
 	 			ArrayAdapter columnHookDetail_Adapter = (ArrayAdapter) Spinner_ColumnHookDetail.getAdapter();
 	 			value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_COLUMN_REINFORCEMENT_HOOK_DETAIL));
 	 			position = columnHookDetail_Adapter.getPosition(value);
 				break; 	 
 				
 			case BEAM_LONGITUDINAL_BAR_SIZE_ADAPTOR_ID:
 				
 	 			ArrayAdapter beamBarSize_Adapter = (ArrayAdapter) Spinner_BeamLongitudinaBarsSize.getAdapter();
 	 			value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_REINFORCEMENT_LONGITUDINAL_BAR_SIZE));
 	 			position = beamBarSize_Adapter.getPosition(value);
 				break; 	 
 				
 			case BEAM_BAR_TEXTURE_ADAPTOR_ID:
 				
 	 			ArrayAdapter beamBarTexture_Adapter = (ArrayAdapter) Spinner_BeamBarTexture.getAdapter();
 	 			value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_REINFORCEMENT_BAR_TEXTURE));
 	 			position = beamBarTexture_Adapter.getPosition(value);
 				break; 	 
 				
 			case BEAM_HOOK_DETAIL_ADAPTOR_ID :
 				
 	 			ArrayAdapter beamHookDetail_Adapter = (ArrayAdapter) Spinner_BeamHookDetail.getAdapter();
 	 			value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BEAM_REINFORCEMENT_HOOK_DETAIL));
 	 			position = beamHookDetail_Adapter.getPosition(value);
 				break; 	  				
 				
 			default:				
 				break;
 			 		 		 		
 		}							
 		
 		return position;
 	}
              
} 